package org.freespace.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.freespace.Constant;
import org.freespace.model.Machine;
import org.freespace.mapper.MachineMapper;
import org.freespace.service.IMachineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.freespace.utils.IpUtil;
import org.freespace.utils.SessionUtil;
import org.freespace.utils.SpringBeanUtil;
import org.freespace.utils.enums.MachineStatus;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 压测机器表 服务实现类
 * </p>
 *
 * @author liuqi
 * @since 2023-05-08
 */
@Service("machineService")
public class MachineServiceImpl extends ServiceImpl<MachineMapper, Machine> implements IMachineService {

    @Override
    public List<Machine> getIdleMachine() {
        QueryWrapper<Machine> wrapper = new QueryWrapper<>();
        wrapper.eq("status", MachineStatus.IDLE.getType());
        return list(wrapper);
    }

    @Override
    public void upgrade(int id) throws Exception {
        Map<String, Object> req = new HashMap<>();
        req.put("callback", String.format(Constant.callbackUrl, IpUtil.getIpAddress(), "8080"));
        req.put("downloaUrl", Constant.fileServerUrl + "machine/apache-jmeter-5.5.zip");

        //更新压力机状态为升级中
        Machine machine = getById(id);
        machine.setStatus(MachineStatus.UPGRADE.getType());
        machine.setLastUpdateBy(SessionUtil.getCurrentUser());
        machine.setLastUpdateTime(LocalDateTime.now());
        updateById(machine);

        if ("dev".equalsIgnoreCase(SpringBeanUtil.getActiveProfile())) {
            HttpUtil.post(String.format(Constant.upgradeUrl, "127.0.0.1", "7777"), JSONUtil.toJsonStr(req), -1);
        } else {
            HttpUtil.post(String.format(Constant.upgradeUrl, machine.getIp(), "7777"), JSONUtil.toJsonStr(req), -1);
        }
    }

    @Override
    public void updateMachineStatus(String ip, Integer status, Map<String, String> map) throws Exception {
        LambdaQueryWrapper<Machine> w = Wrappers.<Machine>lambdaQuery().eq(Machine::getIp, ip);
        Machine machine = getOne(w);

//        if(map.containsKey("cpuInfo") && map.containsKey("memInfo")) {
//
//        }
        machine.setStatus(status);
        machine.setLastUpdateTime(LocalDateTime.now());
        updateById(machine);
    }

    @Override
    public void restart(int id) {
        Machine machine = getById(id);
        machine.setLastUpdateTime(LocalDateTime.now());
        machine.setLastUpdateBy(SessionUtil.getCurrentUser());
        machine.setStatus(MachineStatus.RESTARTING.getType());
        updateById(machine);

        Map<String, Object> req = new HashMap<>();
        req.put("callback", String.format(Constant.callbackUrl, IpUtil.getIpAddress(), "8080"));
        HttpUtil.post(String.format(Constant.restartUrl, machine.getIp(), "7777"), JSONUtil.toJsonStr(req), -1);
    }
}
